﻿using Management.API.Application.Controller.Management.Product;
using Management.API.DTOs.Management;
using MediatR;
using AutoMapper;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Errorcode.ErrorCode;


namespace Management.API.Application.CommandHandler.Management.Product
{
    public class GetProductAttributeCommandHandler : IRequestHandler<GetProductAttributeCommand, ApiResult<IList<GetProductAttributeDTO>>>
    {
        private readonly IProductAttributeRepository iProductAttributeRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetProductAttributeCommandHandler> _logger;

        public GetProductAttributeCommandHandler(IProductAttributeRepository IProductAttributeRepository,
             IMapper mapper, ILogger<GetProductAttributeCommandHandler> logger)
        {
            iProductAttributeRepository = IProductAttributeRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetProductAttributeDTO>>> Handle(GetProductAttributeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iProductAttributeRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetProductAttributeDTO>>(useList);
                return ApiResult<IList<GetProductAttributeDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetProductAttributeCommandHandler");
                throw;
            }

        }



    }
}
